<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var Person = {
      name:'张三',
      run:function() {
        return this.name;
      }
    }
    console.log(Person.run())// 张三
    // this的指向可以变得
    var Person1 = {
      name:'张三',
      run:function() {
        return this.name;
      }
    }
    var Person2 = {
      name:'李四'
    }
    Person2.run = Person1.run
    console.log(Person2.run())// 李四
    // 相当于以下写法
    function fn() {
      return this.name;
    }
    var A = {
      name:'张三',
      run:fn
    }
    var B = {
      name:'李四',
      run:fn
    }
    // 顶层对象
    var obj = {
      name:'王五',
      fun: function() {
        return this.name
      },
    }
    var name = '刘六';
    var fn2 = obj.fun;
    console.log(fn2()) // 刘六
    // 使用场合
    // 全局环境
    function jump() {
      console.log(this === window) // true
    }
    jump();
    // 构造函数
    var Test = function test(p) {
      this.p = p
    }
    var o = new Test("hello")
    console.log(o.p) // hello

    // 对象的方法
    var name2 = "name2"
    var test = {
      name2:'test',
      jump1:function() {
        console.log(this.name2) // test
      }
    }
    test.jump1()
    // (test.jump1())()
    // 避免多层 this
    // var objMore = {
    //   p:function() {
    //     console.log(this)
    //     var p3 = function() {
    //       console.log(this)
    //     }();
    //   }
    // }
    // objMore.p(); // Object , window
    // 实际查分写法如下
    var p3 = function () {
      console.log(this)
    }
    var objMore = {
      p:function() {
        console.log(this)
        var p4 = p3();
      }
    }
    objMore.p();
  </script>
</body>
</html>